package com.chixing.controller;


import com.chixing.entity.*;
import com.chixing.service.*;
import com.chixing.util.Result;
import com.chixing.util.TokenUtil;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


@Controller
@RequestMapping("/orders")
public class OrdersController {
    @Autowired
    private IOrdersService ordersServiceImpl;
    @Autowired
    private ICarService carServiceImpl;
    @Autowired
    private ICustomerService customerServiceImpl;
    @Autowired
    private ICouponAllService couponAllServiceImpl;
    @Autowired
    private IBusinessOutletsService businessOutletsServiceImpl;
    @Autowired
    private ICustomerCouponService getCustomerCouponServiceImpl;

    @Resource
    private AmqpTemplate rabbitTemplate;

    @PostMapping("/myorder")
    public ModelAndView save(@RequestParam("carId") Integer carId,
                             @RequestParam("outletsId") Integer outletsId,
                             @RequestParam("carPrice") Double carPrice,
                             @RequestParam("couponFree") Double couponFree,
                             @RequestParam("couponNo") String couponNo,
                             @RequestParam("fwPrice") Double fwPrice,
                             @RequestParam("custId") Integer custId){

        //创建订单
        Orders orders = new Orders();
        orders.setCarId(carId);
        orders.setCustId(custId);
        orders.setCouponNo(couponNo);
        orders.setOrderPickupAddress(businessOutletsServiceImpl.getBusinessOutletById(outletsId).getOutlesLocal());
        orders.setOrderPickupShop(businessOutletsServiceImpl.getBusinessOutletById(outletsId).getOutletsName());
        orders.setOrderDropoffAddress(businessOutletsServiceImpl.getBusinessOutletById(outletsId).getOutlesLocal());
        orders.setOrderDropoffShop(businessOutletsServiceImpl.getBusinessOutletById(outletsId).getOutletsName());
        orders.setCustTel(customerServiceImpl.getTelById(custId));
        orders.setCustCertificatesNumber(customerServiceImpl.getCustCerNumById(custId));
        orders.setCustDriverLicense(customerServiceImpl.getCustDriverLicense(custId));
        orders.setOrderAmount(1.0);  //订单数量
        orders.setOrderNo(UUID.randomUUID().toString().replace("-",""));

        String orderPickUp = "2023-12-21 12:00:00";
        //1.具有转换功能的对象
        DateTimeFormatter df1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime start = LocalDateTime.parse(orderPickUp,df1);
        orders.setOrderPickupTime(start);

        String orderDropOff = "2023-12-22 12:00:00";
        //1.具有转换功能的对象
        DateTimeFormatter df2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime end = LocalDateTime.parse(orderDropOff,df2);
        orders.setOrderDropoffTime(end);

        orders.setOrderCreatetime(LocalDateTime.now());
        orders.setOrderPrice(carPrice+20-couponFree+fwPrice);
        orders.setCouponNo(couponNo);
        orders.setOrderStatus(0);  //0:待付款  1：已完成 2：已取消

        //订单持久化
        Result result = ordersServiceImpl.createOrder(orders);
        ModelAndView mav = new ModelAndView();
        mav.addObject("result",result);
        mav.setViewName("myOrder/myOrder_confirm");

        //发送消息给队列
        Map<String,Object> map = new HashMap<>();
        map.put("orders",orders);
        map.put("carId",carId);
        rabbitTemplate.convertAndSend("normalExchange","key1",map);

        return mav;
    }



//    @PostMapping("/create")
//    public ModelAndView confirmOrder(Integer custId,Integer carId,Integer outletsId,Integer buyCount){
//
//        Car car = carServiceImpl.getByCarId(carId);
//
//        List<CustomerCoupon> customerCouponList = customerCouponServiceImpl.getValidCoupon(custId);
//
//        BusinessOutlets businessOutlet = businessOutletsServiceImpl.getById(outletsId);
//
//        ModelAndView mav = new ModelAndView("myOrder/myorder_confirm");
//        mav.addObject("car",car);
//        mav.addObject("businessOutlet",businessOutlet);
//        mav.addObject("customerCouponList",customerCouponList);
//        mav.addObject("buyCount",buyCount);
//        mav.addObject("orderOriginPrice",car.getCarPrice()*buyCount);
//
//        return mav;
//    }


   //根据id查询该id所有订单
    @GetMapping("/getOrdersByCustomerId/{token}")
    @ResponseBody
    public ModelAndView getOrdersByCustomerId(@PathVariable("token") String token){
            LoginCustomer loginCustomer = TokenUtil.parseToken(token);
            Integer customerId = loginCustomer.getCustId();
            ModelAndView mav = new ModelAndView();
            mav.addObject("ordersById",ordersServiceImpl.getOrdersByCustomerId(customerId));
            mav.setViewName("customer/personalCenter-MyOrler");
            return mav;
    }

    //根据用户与不同状态值 查询订单
    @GetMapping("/getOrdersByCustomerIdAndStatus/{token}/{orderStatus}")
    @ResponseBody
    public ModelAndView getOrdersByCustomerIdAndStatus(@PathVariable("token") String token,@PathVariable("orderStatus") Integer orderStatus){
        LoginCustomer loginCustomer = TokenUtil.parseToken(token);
        Integer customerId = loginCustomer.getCustId();
        ModelAndView mav = new ModelAndView();
        mav.addObject("ordersById",ordersServiceImpl.getOrdersByCustomerIdAndStatus(customerId,orderStatus));
        mav.setViewName("customer/personalCenter-MyOrler");
        return mav;
    }


    //根据订单编号查出该订单
    @GetMapping("/getOrderByNum/{orderByNum}")
    @ResponseBody
    public ModelAndView getOrder(@PathVariable("orderByNum") String ordersNum){
        ModelAndView mav = new ModelAndView();
        mav.addObject("order",ordersServiceImpl.getOrderByOrderNum(ordersNum).getData());
        mav.setViewName("customer/personalCenter-Orlerpay");
        return mav;
    }

    //跳转待支付
    @GetMapping("/getwaitPay/{orderByNum}")
    @ResponseBody
    public ModelAndView getwaitPay(@PathVariable("orderByNum") String ordersNum){
        ModelAndView mav = new ModelAndView();
        mav.addObject("order",ordersServiceImpl.getOrderByOrderNum(ordersNum).getData());
        mav.setViewName("customer/personalCenter-OrlerIn");
        return mav;
    }



}
